package com.aube.plugin.rank.view;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aube.plugin.rank.R;

/**
 * Created by huyaonan on 16/8/12.
 */
public class RankRefreshView extends FrameLayout {

    private Paint mPaint;

    private TextView mText;
    private ImageView mImg;

    private static final int COLOR = 0x47ffffff;
    private int padding = 0;

    public RankRefreshView(Context context) {
        this(context, null);
    }

    public RankRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RankRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews();
    }

    public void initViews() {
        mPaint = new Paint();
        mPaint.setColor(COLOR);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2);

        mText = new TextView(getContext());
        mText.setTextSize(10);
        mText.setTextColor(COLOR);
        mText.setText("刷新");
        addView(mText);

        mImg = new ImageView(getContext());
        mImg.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon_refresh));
        addView(mImg);

        padding = getResources().getDimensionPixelSize(R.dimen.refresh_padding);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(mText != null && mImg != null) {
            int height = 0, width=0;

            mText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            width += mText.getMeasuredWidth() + padding;
            height = mText.getMeasuredHeight();

            mImg.measure(MeasureSpec.makeMeasureSpec(height-padding, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height-padding, MeasureSpec.EXACTLY));
            width += mImg.getMeasuredWidth();
            height += 2*padding;

            width += height;

            setMeasuredDimension(width, height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int height = getHeight();
        int mOffsetX = height/2;
        mText.layout(mOffsetX, (height-mText.getMeasuredHeight())/2, mOffsetX+mText.getMeasuredWidth(), (height+mText.getMeasuredHeight())/2);
        mOffsetX += padding + mText.getMeasuredWidth();
        mImg.layout(mOffsetX, (height-mImg.getMeasuredHeight())/2, mOffsetX+mImg.getMeasuredWidth(), (height+mImg.getMeasuredHeight())/2);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        RectF rectF = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rectF, height/2, height/2, mPaint);
        if(isLoading) {
            //20s左右就不要转了
            if(angle > 12000) {
                angle = 0;
                isLoading = false;
            }
            mImg.setRotation(angle);
            angle += VALUE;
            invalidate();
        }
    }

    private boolean isLoading = false;
    private int angle = 0;
    private static final int VALUE = 10;
    public void startLoading() {
        if(isLoading)
            return;
        isLoading = true;
        invalidate();
    }

    public void endLoading() {
        angle = 0;
        isLoading = false;
        mImg.setRotation(angle);
    }

}
